package cn.itcast.cd.crud;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipInputStream;


import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.junit.Test;

public class JBPMCRUD {

	ProcessEngine engine = new Configuration().setResource("jbpm.cfg.xml").buildProcessEngine();
	
	
	//-1:还是布署流程.
	@Test
	public void deployByClasspath(){
		engine.getRepositoryService().createDeployment()
			.addResourceFromClasspath("hello.jpdl.xml")
			.addResourceFromClasspath("hello.png")
			.deploy();
	}
	
	@Test
	public void deployByZip() throws Exception{
		
		//通过加载ZIP包来布署流程.
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("hello.zip");
		ZipInputStream zip = new ZipInputStream(in);
		engine.getRepositoryService().createDeployment()
			.addResourcesFromZipInputStream(zip)
			.deploy();
	}
	
	 @Test
	 public void startTask(){
		 
		 //每次开始一个任务后,就产生了一个流程的实例,它是惯穿在整个任务当中的,当流程走到哪一步了,它就在哪儿 .
		 ProcessInstance process = engine.getExecutionService().startProcessInstanceByKey("hello");
		 System.out.println(process.getId());
		 System.out.println(process.getName());
		 System.out.println(process.getKey());
		 System.out.println(process.getState());
	 }
	 
	 @Test
	 public void viewPD(){
		 //创建流程定义对应的查询对象.ProcessDefinitionQuery.这个对象可以查询出有多少流程(就是我们画的图);
		 
		 //这里面的方法基本都 是采用的链式编程方式.这个查询对象还提供了分页和排序等数据库的操作.
		 List<ProcessDefinition> pds = engine.getRepositoryService().createProcessDefinitionQuery()
		 	.processDefinitionKey("hello").list();
		 
		 for (ProcessDefinition processDefinition : pds) {
			 
			 //deploymentId是发布的流程的ID.
			System.out.println(processDefinition.getDeploymentId());
			System.out.println(processDefinition.getId());
			System.out.println(processDefinition.getKey());
			System.out.println(processDefinition.getName());
			System.out.println(processDefinition.getVersion());
		}
	 }
	 
	 @Test
	 public void deletePD(){
		 
		 String deployId = "80001";
		 //删除定义了的流程.这个方法是普通的删除.不会删除历史等信息.并且如果要删除的流程当前有任务在执行.会报错.
		 //engine.getRepositoryService().deleteDeployment(deployId);
		 
		 //下面这种删除 是级联删除,会删除历史等信息.并且,不管当前流程有没有任务在执行.也不会报错.所以相当暴力.
		 engine.getRepositoryService().deleteDeploymentCascade(deployId);
	 }
	 
	 @Test
	 public void viewPDImage() throws Exception{
		 
		 //得到流程图的仓库服务对象.通过它我们可以得到流程图,xml文件.
		 RepositoryService repository =  engine.getRepositoryService();
		 String deploymentId = "90001";
		 String resourceName = "hello.png";
		 
		 InputStream in = repository.getResourceAsStream(deploymentId, resourceName);
		 OutputStream out = new FileOutputStream("D:\\hello.png");
		 byte[] buf = new byte[1024*4];
		 int i = 0;
		 //这里将取出来的数据读到一个数组的缓冲里面去.
		 while((i=in.read(buf))!=-1){
			 out.write(buf,0,i);
		 }
		 in.close();
		 out.close();
	 }
	 
	 
}
